package com.nchu.domain.dto;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nchu.domain.po.User;
import com.nchu.domain.vo.UserVO;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.Collections;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

@Data
@ApiModel(description = "分页结果")
public class PageDTO<T> {
    @ApiModelProperty("总条数")
    private Long total;
    @ApiModelProperty("总页数")
    private Long pages;
    @ApiModelProperty("集合")
    private List<T> list;


    public static <V,P> PageDTO<V> of(Page<P> p, Class<V> voClass){
        PageDTO<V>  dto = new PageDTO<>();

        dto.setTotal(p.getTotal());
        dto.setPages(p.getPages());

        List<P> records = p.getRecords();
        if (CollUtil.isEmpty(records)){
            dto.setList(Collections.emptyList());
            return dto;
        }
        //数据转换
        dto.setList(BeanUtil.copyToList(records,voClass));
        return dto;
    }


    public static <V,P> PageDTO<V> of(Page<P> p, Function<P,V> function){
        PageDTO<V>  dto = new PageDTO<>();

        dto.setTotal(p.getTotal());
        dto.setPages(p.getPages());

        List<P> records = p.getRecords();
        if (CollUtil.isEmpty(records)){
            dto.setList(Collections.emptyList());
            return dto;
        }
        //数据转换
        dto.setList(records.stream().map(function).collect(Collectors.toList()));
        return dto;
    }
    
    
}
